package com.qxy.hgz.rank.adapter;

import android.view.LayoutInflater;
import android.view.ViewGroup;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.bumptech.glide.Glide;
import com.qxy.hgz.R;
import com.qxy.hgz.databinding.RecycleViewItemBinding;
import com.qxy.hgz.rank.model.TVSeries;
import com.qxy.hgz.rank.model.Variety;
import com.qxy.hgz.rank.model.Video;

import java.util.List;

public class FilmViewAdapter<T extends Video> extends RecyclerView.Adapter<FilmViewAdapter.ViewHolder> {

    private final List<T> list;
    public FilmViewAdapter(List<T> videoList) {
        this.list = videoList;
    }
    public void addListToAdapter(List<T> list) {
        this.list.addAll(list);
    }


    @NonNull
    @Override
    public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
       RecycleViewItemBinding binding =
               RecycleViewItemBinding.inflate(LayoutInflater.from(parent.getContext()),parent,false);
        return new ViewHolder(binding);
    }

    @Override
    public void onBindViewHolder(@NonNull FilmViewAdapter.ViewHolder holder, int position) {
        T video = list.get(position);
        fillData(holder,video);
    }


    //这是一个值得优化的方法
    private void fillData(@NonNull ViewHolder holder, T video) {
        holder.binding.filmTitle.setText(video.getName());
        List<String> actors = video.getActors();
        List<String> directors = video.getDirectors();
        StringBuilder sb = new StringBuilder();
        if (video instanceof TVSeries) {
            String enName = ((TVSeries) video).getEnName();
            if (enName != null && !enName.isEmpty()) {
                sb.append(enName).append("\n");
            }
            List<String> tags = ((TVSeries) video).getTag();
            if (tags != null && tags.size() > 0) {
                for (int i = 0;i<tags.size();i++) {
                    sb.append(tags.get(i)).append(" ");
                }
                sb.append("\n");
            }
            holder.binding.pay.setText(R.string.tv_series);


        }

        if (video instanceof Variety) {
            String enName = ((Variety) video).getEnName();
            if (enName != null && !enName.isEmpty()) {
                sb.append(enName).append("\n");
            }
            holder.binding.pay.setText(R.string.tv_series);
        }
        if (actors != null) {
            sb.append("演员：");
            int len = Math.min(actors.size(), 3);
            for (int i = 0; i<len;i++) {
                sb.append(actors.get(i)).append(" ");
            }
            sb.append("\n");
        }
        if (directors != null) {
            sb.append("导演：");
            int len = Math.min(directors.size(), 3);
            for (int i = 0; i<len;i++) {
                sb.append(directors.get(i)).append(" ");
            }
            sb.append("\n");
        }
        sb.append(video.getReleaseDate());
        holder.binding.filmInfo.setText(sb.toString());
        if (video.getHot() != null){
            double value = (video.getHot() / 1000.0);
            holder.binding.hotValue.setText(value + "万");
        }else {
            holder.binding.hotValue.setText("0");
        }
        if (video.getImageUrl() != null && !video.getImageUrl().isEmpty()) {
            Glide.with(holder.binding.getRoot())
                    .load(video.getImageUrl())
                    .into(holder.binding.imageView);
        }else {
            Glide.with(holder.binding.getRoot())
                    .load("https://iph.href.lu/738x1032")
                    .into(holder.binding.imageView);
        }
    }

    @Override
    public int getItemCount() {
        return list.size();
    }

    public static class ViewHolder extends RecyclerView.ViewHolder {
        private final RecycleViewItemBinding binding;

        public ViewHolder(@NonNull RecycleViewItemBinding binding) {
            super(binding.getRoot());
            this.binding = binding;
        }
    }
}
